<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        @import url('https://fonts.googleapis.com/css?family=Lato&display=swap');
        * {
            box-sizing: border-box;
        }
        
        body {
            background-color: #242333;
            color: #fff;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            height: 100vh;
            font-family: 'Lato', sans-serif;
            margin: 0;
        }
        
        .movie-container {
            margin: 20px 0;
        }
        
        .movie-container select {
            background-color: #fff;
            border: 0;
            border-radius: 5px;
            font-size: 14px;
            margin-left: 10px;
            padding: 5px 15px 5px 15px;
            -moz-appearance: none;
            -webkit-appearance: none;
            appearance: none;
        }
        
        .container {
            perspective: 1000px;
            margin-bottom: 30px;
        }
        
        .seat {
            background-color: #444451;
            height: 12px;
            width: 15px;
            margin: 3px;
            border-top-left-radius: 10px;
            border-top-right-radius: 10px;
        }
        
        .seat.selected {
            background-color: #6feaf6;
        }
        
        .seat.occupied {
            background-color: #fff;
        }
        
        .seat:nth-of-type(2) {
            margin-right: 18px;
        }
        
        .seat:nth-last-of-type(2) {
            margin-left: 18px;
        }
        
        .seat:not(.occupied):hover {
            cursor: pointer;
            transform: scale(1.2);
        }
        
        .showcase .seat:not(.occupied):hover {
            cursor: default;
            transform: scale(1);
        }
        
        .showcase {
            background: rgba(0, 0, 0, 0.1);
            padding: 5px 10px;
            border-radius: 5px;
            color: #777;
            list-style-type: none;
            display: flex;
            justify-content: space-between;
        }
        
        .showcase li {
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0 10px;
        }
        
        .showcase li small {
            margin-left: 2px;
        }
        
        .row {
            display: flex;
        }
        
        .screen {
            background-color: #fff;
            height: 70px;
            width: 100%;
            margin: 15px 0;
            transform: rotateX(-45deg);
            box-shadow: 0 3px 10px rgba(255, 255, 255, 0.7);
        }
        
        p.text {
            margin: 5px 0;
        }
        
        p.text span {
            color: #6feaf6;
        }
    </style>
</head>

<body>
    <div class="movie-container">
        <label>选一部电影:</label>
        <select id="movie">
          <option value="10">复仇者：终局($10)</option>
          <option value="12">小丑 ($12)</option>
          <option value="8">玩具总动员4 ($8)</option>
          <option value="9">狮子王 ($9)</option>
        </select>
    </div>

    <ul class="showcase">
        <li>
            <div class="seat"></div>
            <small>空位</small>
        </li>
        <li>
            <div class="seat selected"></div>
            <small>选择</small>
        </li>
        <li>
            <div class="seat occupied"></div>
            <small>占位</small>
        </li>
    </ul>

    <div class="container">
        <div class="screen"></div>

        <div class="row">
            <div class="seat"></div>
            <div class="seat"></div>
            <div class="seat"></div>
            <div class="seat"></div>
            <div class="seat"></div>
            <div class="seat"></div>
            <div class="seat"></div>
            <div class="seat"></div>
        </div>
        <div class="row">
            <div class="seat"></div>
            <div class="seat"></div>
            <div class="seat"></div>
            <div class="seat occupied"></div>
            <div class="seat occupied"></div>
            <div class="seat"></div>
            <div class="seat"></div>
            <div class="seat"></div>
        </div>
        <div class="row">
            <div class="seat"></div>
            <div class="seat"></div>
            <div class="seat"></div>
            <div class="seat"></div>
            <div class="seat"></div>
            <div class="seat"></div>
            <div class="seat occupied"></div>
            <div class="seat occupied"></div>
        </div>
        <div class="row">
            <div class="seat"></div>
            <div class="seat"></div>
            <div class="seat"></div>
            <div class="seat"></div>
            <div class="seat"></div>
            <div class="seat"></div>
            <div class="seat"></div>
            <div class="seat"></div>
        </div>
        <div class="row">
            <div class="seat"></div>
            <div class="seat"></div>
            <div class="seat"></div>
            <div class="seat occupied"></div>
            <div class="seat occupied"></div>
            <div class="seat"></div>
            <div class="seat"></div>
            <div class="seat"></div>
        </div>
        <div class="row">
            <div class="seat"></div>
            <div class="seat"></div>
            <div class="seat"></div>
            <div class="seat"></div>
            <div class="seat occupied"></div>
            <div class="seat occupied"></div>
            <div class="seat occupied"></div>
            <div class="seat"></div>
        </div>
    </div>

    <p class="text">
        您已选择 <span id="count">0</span> 票价格为$<span id="total">0</span
        >
      </p>
</body>
</html>
<script>
   const container = document.querySelector('.container');
      const seats = document.querySelectorAll('.row .seat:not(.occupied)');
      const count = document.getElementById('count');
      const total = document.getElementById('total');
      const movieSelect = document.getElementById('movie');

populateUI();

let ticketPrice = +movieSelect.value;  //加+代表Number,不加代表字符串


// // 电影下拉框事件监听
movieSelect.addEventListener('change', e => {  //当元素的值发生改变时，会发生 change 事件。
  ticketPrice = +e.target.value;      //意思是:某一个选项的值强行变成Number赋值给票价
  setMovieData(e.target.selectedIndex, e.target.value);
  updateSelectedCount();
});



// 座位点击事件
container.addEventListener('click', e => {
  if (               //classList 属性返回元素的类名
    e.target.classList.contains('seat') &&   //意识是如果e.target标签中包含seat的话并且e.target中不包含occupied
    !e.target.classList.contains('occupied')
  ) {
    e.target.classList.toggle('selected');   //toggle()为连续点击事件

    updateSelectedCount();
  }
});

// 初始计数和集合总数
updateSelectedCount();

// 保存选定的电影索引和价格
function setMovieData(movieIndex, moviePrice) {   
  localStorage.setItem('selectedMovieIndex', movieIndex);
  localStorage.setItem('selectedMoviePrice', moviePrice);  
  //存储JSON对象时会发现浏览器存储的内容为[object,object]
}

// 更新总数和计数
function updateSelectedCount() {
  const selectedSeats = document.querySelectorAll('.row .seat.selected'); 
  //JSON.stringify(object)进行类型转换

  const seatsIndex = [...selectedSeats].map(seat => [...seats].indexOf(seat));  //点击的座位
  //console.log(...seats);
  // 这以上的两句的意思是:点击的[...selectedSeats].map(seat)在[...seats].indexOf(seat)中第一次出现的索引值.

  localStorage.setItem('selectedSeats', JSON.stringify(seatsIndex));

  const selectedSeatsCount = selectedSeats.length;   //点击的数量

  count.innerText = selectedSeatsCount;   //数量
  total.innerText = selectedSeatsCount * ticketPrice;

  setMovieData(movieSelect.selectedIndex, movieSelect.value);
  //console.log(e.target.selectedIndex,e.target.value);//index从0开始,值为value
}

// 从localstorage获取数据并渲染
function populateUI() {
  const selectedSeats = JSON.parse(localStorage.getItem('selectedSeats'));

  if (selectedSeats !== null && selectedSeats.length > 0) {
    seats.forEach((seat, index) => {
      if (selectedSeats.indexOf(index) > -1) {
        seat.classList.add('selected');
      }
    });
  }

  const selectedMovieIndex = localStorage.getItem('selectedMovieIndex');

  if (selectedMovieIndex !== null) {
    movieSelect.selectedIndex = selectedMovieIndex;
  }
}

</script>